package com.dhq.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class TestConsumer {
    public static void main(String[] args) throws Exception {
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = connectionFactory.createConnection();

        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic("频道1");
        MessageConsumer consumer = session.createConsumer(topic);

        /**
         * 接受消息
         */
        consumer.setMessageListener(new MessageListener() {
            public void onMessage(Message message) {
                TextMessage message1 = (TextMessage) message;
                String text;
                try {
                    text=message1.getText();
                    System.out.println("消费者1*********** message content:************"+text);
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        });

        //等待不让消费者挂掉
        System.in.read();

        consumer.close();
        session.close();
        connection.close();
    }
}
